Skip to content

Fix #624: preserve duplicate output column names in query results#722

Closed
RamiNoodle733 wants to merge 1 commit into
simonw:mainfrom
RamiNoodle733:fix/issue-624
Closed

Fix #624: preserve duplicate output column names in query results#722
RamiNoodle733 wants to merge 1 commit into
simonw:mainfrom
RamiNoodle733:fix/issue-624

Conversation

@RamiNoodle733

@RamiNoodle733 RamiNoodle733 commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

Fixes:

Summary

When a query returns duplicate column names (for example joining two tables with the same field names), Database.query() and related helpers used dict(zip(...)), which silently overwrote earlier values.

This change preserves all values by suffixing duplicate keys as _2, _3, etc.

Changes

  • Added _row_to_dict() helper to safely map row values to unique dictionary keys
  • Updated Database.query(), Queryable.rows_where(), and Table.search() to use it
  • Added regression test covering duplicate output columns

Validation

  • pytest -q tests/test_query.py tests/test_rows.py
  • ruff check sqlite_utils/db.py tests/test_query.py

simonw added a commit that referenced this pull request Jul 6, 2026
Queries returning duplicate column names - e.g. joins between tables
sharing column names - silently lost values because rows were built
with dict(zip(keys, row)), where the last duplicate wins.

Later occurrences are now renamed with a numeric suffix: id, id
becomes id, id_2 - skipping any suffix that would collide with a
real column in the same query.

The new utils.dedupe_keys() helper transforms the key list once per
query, so the per-row dict construction is unchanged and there is no
measurable performance impact.

Applied in Database.query() (including the PRAGMA and RETURNING
paths), Table.rows_where(), Table.search() and the CLI's JSON output.
CSV, TSV and table output keep the original duplicate headers.

Refs #722

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@simonw

simonw commented Jul 6, 2026

Copy link
Copy Markdown
Owner

@simonw simonw closed this Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants